package com.jingdianjichi.auth.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.convert.AuthRoleDTOConverter;
import com.jingdianjichi.auth.domin.entity.AuthRoleBO;
import com.jingdianjichi.auth.domin.service.AuthRoleDomainService;
import com.jingdianjichi.auth.dto.AuthRoleDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;



    /**
     * @Description: 新增角色
     * @return:
     * @Author:  sky
     * @date:  2024/11/11 16:09
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(authRoleDTO));
            }

            // 字符串需要同时检测， null 和 空串 两种情况，断言匹配false抛出异常
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authRoleDomainService.add(authRoleBO));
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * @Description: 删除角色
     * @return:
     * @Author:  sky
     * @date:  2024/11/11 16:09
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(authRoleDTO));
            }

            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authRoleDomainService.delete(authRoleBO));
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    /**
     * @Description: 更新角色信息
     * @return:
     * @Author:  sky
     * @date:  2024/11/11 16:09
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            // 判断是否启动info级别的日志，如果启动了再执行json转换，减少性能开销
            if(log.isInfoEnabled()){
                log.info("log-info:{}", JSON.toJSONString(authRoleDTO));
            }

            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            // alt + enter 快速生成返回变量
            return Result.success(authRoleDomainService.update(authRoleBO));
        }catch (Exception e){
            log.error("log-error:{}",e.getMessage());
            return Result.fail(e.getMessage());
        }
    }




}
